package com.owner.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.relation.Role;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.owner.biz.HusbandryBiz;
import com.owner.biz.InformBiz;
import com.owner.biz.PlotsBiz;
import com.owner.biz.PlotsManageBiz;
import com.owner.biz.PremissionRoleBiz;
import com.owner.biz.RepairsBiz;
import com.owner.biz.ReplyRepairsBiz;
import com.owner.biz.UserBiz;
import com.owner.dao.PlotsManageDao;
import com.owner.entity.House;
import com.owner.entity.Husbandry;
import com.owner.entity.Inform;
import com.owner.entity.Plots;
import com.owner.entity.PlotsManage;
import com.owner.entity.PremissionRole;
import com.owner.entity.Repairs;
import com.owner.entity.ReplyRepairs;
import com.owner.entity.User;
@Controller
public class ShowJspContoller {
	@Autowired
	private RepairsBiz repairsBiz;
	@Autowired
	private PlotsManageBiz plotsManageBiz;
	@Autowired
	private ReplyRepairsBiz replyRepairsBiz;
	@Autowired
	private PlotsBiz plotsBiz;
	@Autowired
	private InformBiz informBiz;
	@Autowired
	private UserBiz userBiz;
	@Autowired
	private HusbandryBiz husbandryBiz;
	@Autowired
	private PremissionRoleBiz premissionRoleBiz;
	
	@RequestMapping("/House")
	public String house(){
		return "house";
	}
	
	@RequestMapping("/Complain")//投诉
	public String complain(Model model,HttpSession httpSession,Integer page){
		page = page == null ? 1 : page;
		int pageSize = 5;
		int startIndex = (page - 1) * pageSize;
		//int[] array={1,2};
//		List<Repairs> list = repairsBiz.getAllRepairsByType(1,array);
//		model.addAttribute("ComplaintList",list);
		User user=(User) httpSession.getAttribute("user");
		System.out.println("民资："+user.getId());
		List<PlotsManage> PMlist=plotsManageBiz.getAllPlotsByUserId(user.getId());
		int[] array=new int[20];
		for(int i=0;i<PMlist.size();i++){
			//array[i]=;
			System.out.println(PMlist.get(i).getPlots().getId());
			array[i]=PMlist.get(i).getPlots().getId();
		}	
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("type",0);
		map.put("ids",array);
		
		int rows=repairsBiz.countPage(map).size();
		map.put("startIndex", startIndex);
		map.put("pageSize", pageSize);
		
		List<Repairs> list=repairsBiz.getAllRepairsByType(map);
		
		int pageCount = (int) Math.ceil((double) rows / pageSize);
		model.addAttribute("page", page);
		model.addAttribute("totalPages", pageCount);
		
		model.addAttribute("ComplaintList",list);
		return "complain";
	}
	
	@RequestMapping("/Repairs")//报修
	public String repairs(Model model,HttpSession httpSession,Integer page){
		page = page == null ? 1 : page;
		int pageSize = 5;
		int startIndex = (page - 1) * pageSize;
		
		User user=(User) httpSession.getAttribute("user");
		System.out.println("民资："+user.getId());
		List<PlotsManage> PMlist=plotsManageBiz.getAllPlotsByUserId(user.getId());
		int[] array=new int[20];
		for(int i=0;i<PMlist.size();i++){
			//array[i]=;
			System.out.println(PMlist.get(i).getPlots().getId());
			array[i]=PMlist.get(i).getPlots().getId();
		}	
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("type",1);
		map.put("ids",array);
		
		int rows=repairsBiz.countPage(map).size();
		map.put("startIndex", startIndex);
		map.put("pageSize", pageSize);
		
		List<Repairs> list=repairsBiz.getAllRepairsByType(map);
		
		int pageCount = (int) Math.ceil((double) rows / pageSize);
		model.addAttribute("page", page);
		model.addAttribute("totalPages", pageCount);
		
		model.addAttribute("RepairsList",list);
		return "repairs";
	}
	@RequestMapping("/ReplyComplaints")//根据id显示投诉
	public String replyComplaints(Model model,Integer rid){
		System.out.println("rid:"+rid);
		Repairs showRepairsByIdListC = repairsBiz.getRepairsById(rid);
		System.out.println("图片：："+showRepairsByIdListC.getImage());
		model.addAttribute("showRepairsByIdListC",showRepairsByIdListC);
		return "replyComplaints";
	}
	@RequestMapping("/ReplyRepair")//根据id显示报修
	public String replyRepair(Model model,Integer rid){
		Repairs showRepairsByIdListR = repairsBiz.getRepairsById(rid);
		model.addAttribute("showRepairsByIdListB",showRepairsByIdListR);
		return "replyRepair";
	}
	
	@RequestMapping("/addReplyRepairs")//保存报修
	public String addReplyRepairs(Model model,HttpSession httpSession,Integer rid,Integer hid,Integer pid,String content,Integer type){
//		Repairs showRepairsByIdListR = repairsBiz.getRepairsById(rid);
//		model.addAttribute("showRepairsByIdListB",showRepairsByIdListR);
		ReplyRepairs repairs=new ReplyRepairs();
		Repairs repairs2=new Repairs();
		repairs2.setId(rid);
		repairs.setRepairs(repairs2);
		Plots plots=new Plots();
		plots.setId(pid);
		repairs.setPlots(plots);
		House house =new House();
		house.setId(hid);
		repairs.setHouse(house);
		Date date=new Date();
		repairs.setTime(date);
		repairs.setContent(content);
		repairs.setType(type);
		User user=(User) httpSession.getAttribute("user");
		repairs.setUserName(user.getNickName());
		int re=replyRepairsBiz.addReplyRepairs(repairs);
		if(re>0){
	       System.out.println("加入成功");
	    }else{
	       System.out.println("加入失败");
	     }
		return "index";
	}
	@RequestMapping("/Redact")
	public String redact(Model model,HttpSession httpSession){
		User user=(User) httpSession.getAttribute("user");
		System.out.println("民资："+user.getId());
		List<PlotsManage> PMlist=plotsManageBiz.getAllPlotsByUserId(user.getId());
		System.out.println("plotsManage的个数："+PMlist.size());
		model.addAttribute("PMlist",PMlist);
		return "redact";
	}
	@RequestMapping("/addInform")//保存通知
	public String addInform(Model model,HttpSession httpSession,Integer plotsId,String type,String content){
        Inform inform=new Inform();
        Date date=new Date();
        inform.setTime(date);
        inform.setType(type);
        User user=(User) httpSession.getAttribute("user");
        inform.setUserName(user.getNickName());
        inform.setContent(content);
        Plots plots=new Plots();
        plots.setId(plotsId);
        inform.setPlots(plots);
        
		int re=informBiz.addInform(inform);
		if(re>0){
	       System.out.println("加入成功");
	    }else{
	       System.out.println("加入失败");
	     }
		return "index";
	}
	@RequestMapping("/Inform")
	public String inform(Model model,HttpSession httpSession,Integer page){
		page = page == null ? 1 : page;
		int pageSize = 5;
		int startIndex = (page - 1) * pageSize;
		
		User user=(User) httpSession.getAttribute("user");
		System.out.println("民资："+user.getId());
		
		int rows=informBiz.InformCountPage(user.getNickName()).size();
		
		int pageCount = (int) Math.ceil((double) rows / pageSize);

		model.addAttribute("page", page);
		model.addAttribute("totalPages", pageCount);
		
		List<Inform> informsList=informBiz.getAllInforms(user.getNickName(),startIndex,pageSize);
		model.addAttribute("informsList",informsList);
		return "inform";
	}
	
	@RequestMapping("/Community")
	public String Community(){
		return "Community";
	}
	@RequestMapping("updateUser")
	public String updateUser(Model model,HttpSession httpSession){
		
		return "updateUser";
	}
	@RequestMapping("doupdateUser")
	public String doupdateUser(Model model,HttpSession httpSession,String nickName,String password,String email){
		User user=new User();
		user.setEmail(email);
		user.setNickName(nickName);
		user.setPassword(password);
		System.out.println("密码："+password);
		User user1=(User) httpSession.getAttribute("user");
		user.setId(user1.getId());
		int re=userBiz.updateZUser(user);
		if(re>0){
		       System.out.println("修改成功");
		    }else{
		       System.out.println("修改失败");
		     }
		return "redirect:showlogin";
	}
	
	//唐玉立后台的代码
	@RequestMapping("/PPindex")
	public String PPindex(){
		
		return "PPindex";
	}
	@RequestMapping("/PPcount")
	public String PPcount(Model model){
		int PPuserCount=userBiz.getCountUser();
		int PPploysCount=plotsBiz.getCountPlots();
		int PPRepairsCount=repairsBiz.getCountRepairs();
		int PPRepairsCountT=repairsBiz.getCountRepairsT();
		model.addAttribute("PPuserCount",PPuserCount);
		model.addAttribute("PPploysCount",PPploysCount);
		model.addAttribute("PPRepairsCount",PPRepairsCount);
		model.addAttribute("PPRepairsCountT",PPRepairsCountT);
		return "PPcount";
	}
	@RequestMapping("/PPlist")
	public String PPlist(Model model){
//		List<PlotsManage> list =plotsManageBiz.getPlots(0, null);
//		model.addAttribute("list",list);
//		
//		 List<PlotsManage> list =plotsManageBiz.getPlots(cid, title);
//		 model.addAttribute("listt",list);
		
		return "PPlist";
	}
	@RequestMapping("/PPset")
	public String PPset(Model model){
		PremissionRole premissionRole1=premissionRoleBiz.checkBox(1);
		PremissionRole premissionRole2=premissionRoleBiz.checkBox(2);
		PremissionRole premissionRole3=premissionRoleBiz.checkBox(3);
		PremissionRole premissionRole4=premissionRoleBiz.checkBox(4);
		PremissionRole premissionRole5=premissionRoleBiz.checkBox(5);
		PremissionRole premissionRole6=premissionRoleBiz.checkBox(6);
		PremissionRole premissionRole7=premissionRoleBiz.checkBox(7);
		model.addAttribute("premissionRole1",premissionRole1);
		model.addAttribute("premissionRole2",premissionRole2);
		model.addAttribute("premissionRole3",premissionRole3);
		model.addAttribute("premissionRole4",premissionRole4);
		model.addAttribute("premissionRole5",premissionRole5);
		model.addAttribute("premissionRole6",premissionRole6);
		model.addAttribute("premissionRole7",premissionRole7);
		return "PPset";
	}
	@RequestMapping("/PPsets")
	public String PPsets(Model model,int[] checkbox){
		
		for(int i=1;i<=7;i++){
			premissionRoleBiz.updatePremissionRole(i, 0);
		}
		for(int i=0;i<checkbox.length;i++){
			System.out.println(checkbox[i]);
			premissionRoleBiz.updatePremissionRole(checkbox[i], 1);
		}
		
		return "PPindex";
	}
	@RequestMapping("/PPupdate")
	public String PPupdate(){
		
		return "PPupdate";
	}
	
	@RequestMapping("/PPupdatehome")
	public String PTupdatehome(){
		return "PPupdatehome";
	}
	
	@RequestMapping("/PPhomeupdate")
	public String PPhomeupdate(){
		return "PPhomeupdate";
	}
	
	@RequestMapping("/PPhomelist")
	public String PPhomelist(Model model){
		List<Plots> plotsList=plotsBiz.getAllPlotsName();
		model.addAttribute("plotsList",plotsList);
		return "PPhomelist";
	}
	@RequestMapping("/PPhomeadd")//添加家政
	public String PPhomeadd(Model model,String Hphone,String Hscope,String Hname,Integer plotsId){
		Husbandry husbandry=new Husbandry();
		husbandry.setName(Hname);
		husbandry.setPhone(Hphone);
		husbandry.setScope(Hscope);
		Plots plots=new Plots();
		plots.setId(plotsId);
		husbandry.setPlots(plots);
		int re=husbandryBiz.addHusbandry(husbandry);
		if(re>0){
		       System.out.println("添加家政成功");
		    }else{
		       System.out.println("添加家政失败");
		}
		
		return "PPindex";
	}
	@RequestMapping("/PPhome")//显示家政
	public String PPhome(Model model,String name){
		List<Husbandry> husbandriesList=husbandryBiz.getAllHusbandries(name);
		model.addAttribute("husbandriesList",husbandriesList);
		List<Plots> plotsList=plotsBiz.getAllPlotsName();
		model.addAttribute("plotsList",plotsList);
		return "PPhome";
	}
	@RequestMapping("/PPdetail")
	public String PPdetail(Model model,int id){
//		Plots plots =plotsBiz.getByid(1);
//		System.out.println(plots.getId());
//		model.addAttribute("detail",plots);
		return "PPdetail";
	}
	@RequestMapping("/PPadd")
	public String PPadd(){
		
		return "PPadd";
	}
	
	@RequestMapping("/PPcheckhome")
	public String PPcheckhome(Model model,Integer id){
		Husbandry husbandry=husbandryBiz.getHusbandryById(id);
		model.addAttribute("husbandry",husbandry);
		return "PPcheckhome";
	}
	@RequestMapping("/deleteHusbandry")
	public String deleteHusbandry(Model model,Integer id){
		int re=husbandryBiz.deleteHusbandry(id);
		List<Husbandry> husbandriesList=husbandryBiz.getAllHusbandries("");
		model.addAttribute("husbandriesList",husbandriesList);
		List<Plots> plotsList=plotsBiz.getAllPlotsName();
		model.addAttribute("plotsList",plotsList);
		if(re>0){
		       System.out.println("删除家政成功");
		    }else{
		       System.out.println("删除家政失败");
		}
		
		return "PPhome";
	}
	
	@RequestMapping("/doPPadd")
	public String doPPadd(Model model,HttpSession httpSession,String nickName,String password,String email){
		User user=new User();
		user.setEmail(email);
		user.setNickName(nickName);
		user.setPassword(password);
		com.owner.entity.Role role=new com.owner.entity.Role();
		role.setId(3);
		user.setRole(role);
		int re =userBiz.addZUser(user);
		if(re>0){
		       System.out.println("修改成功");
		    }else{
		       System.out.println("修改失败");
		     }
		
		return "PPindex"; 
	}
	
	@RequestMapping(value="/getAllGenreByName",produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String  getAllGenreByName(Model model,int id){
		List<Husbandry> Husbandrylist=husbandryBiz.getbyId(id);
		System.out.println(husbandryBiz.getbyId(id));
		
		return JSON.toJSONString(Husbandrylist);
	}
	@RequestMapping("/updateHusbandry")
	public String updateHusbandry(Model model,Integer Hid,String Hphone,String Hscope,String Hname,Integer plotsId){
		Husbandry husbandry=new Husbandry();
		husbandry.setId(Hid);
		husbandry.setName(Hname);
		husbandry.setPhone(Hphone);
		husbandry.setScope(Hscope);
		Plots plots=new Plots();
		plots.setId(plotsId);
		husbandry.setPlots(plots);
		int re=husbandryBiz.updateHusbandry(husbandry);
		if(re>0){
		       System.out.println("修改成功");
		    }else{
		       System.out.println("修改失败");
		     }
		List<Husbandry> husbandriesList=husbandryBiz.getAllHusbandries("");
		model.addAttribute("husbandriesList",husbandriesList);
		List<Plots> plotsList=plotsBiz.getAllPlotsName();
		model.addAttribute("plotsList",plotsList);
		return "PPhome"; 
	}
	
	
	
	
}
